//
//  GRFMDownloadManager.m
//  GRFMDownloader_Example
//
//  Created by Guo Rui on 2018/1/12.
//  Copyright © 2018年 GR. All rights reserved.
//

#import "GRFMDownloadManager.h"
#import "NSString+MD5.h"
@interface GRFMDownloadManager()<NSCopying, NSMutableCopying>

@property (strong, nonatomic) NSMutableDictionary *downloadInfo;

@end

@implementation GRFMDownloadManager

static GRFMDownloadManager *_instance;
+ (instancetype)sharedInstance
{
    if (!_instance) {
        _instance = [[self alloc] init];
    }
    return _instance;
}

+ (instancetype)allocWithZone:(struct _NSZone *)zone
{
    if (!_instance) {
        static dispatch_once_t onceToken;
        dispatch_once(&onceToken, ^{
            _instance = [super allocWithZone:zone];
        });
    }
    return _instance;
}

- (id)copyWithZone:(NSZone *)zone
{
    return _instance;
}

- (id)mutableCopyWithZone:(NSZone *)zone
{
    return _instance;
}

#pragma mark - Public
- (void)download:(NSURL *)url info:(DownloadInfoBlock)info progressChange:(DownloadProgressChangeBlock)progressChange stateChange:(DownloadStateChangeBlock)stateChange success:(DownloadSuccessBlock)success faile:(DownloadFailedBlock)faile
{
    NSString *md5 = [url.absoluteString md5];
    GRFMDownloader *downloader = [self.downloadInfo objectForKey:md5];
    if (!downloader) {
        downloader = [[GRFMDownloader alloc] init];
        
        __weak typeof(self) _self = self;
        [downloader download:url
                        info:info
              progressChange:progressChange
                 stateChange:stateChange
                     success:^(NSURL *url, NSString *cachePath) {
                         
                         [_self.downloadInfo removeObjectForKey:md5];
                         
                         if (success) {
                             success(url, cachePath);
                         }
                     }
                       faile:faile];
        
        [self.downloadInfo setObject:downloader forKey:md5];
    }
}

- (void)cancelWithURL:(NSURL *)url
{
    GRFMDownloader *downloader = [self.downloadInfo objectForKey:[url.absoluteString md5]];
    [downloader cancel];
}

- (void)pauseWithURL:(NSURL *)url
{
    GRFMDownloader *downloader = [self.downloadInfo objectForKey:[url.absoluteString md5]];
    [downloader pause];
}

- (void)resumeWithURL:(NSURL *)url
{
    GRFMDownloader *downloader = [self.downloadInfo objectForKey:[url.absoluteString md5]];
    [downloader resume];
}

- (void)resumeAll
{
    [self.downloadInfo.allValues performSelector:@selector(resume)];
}

- (void)cancelAll
{
    [self.downloadInfo.allValues performSelector:@selector(cancel)];
}

- (GRFMDownloaderState)downloadStateWithURL:(NSURL *)url
{
    GRFMDownloader *downloader = [self.downloadInfo objectForKey:[url.absoluteString md5]];
    return downloader.state;
}

- (float)downloadProgressWithURL:(NSURL *)url
{
    GRFMDownloader *downloader = [self.downloadInfo objectForKey:[url.absoluteString md5]];
    return downloader.progress;
}

#pragma mark - Getter
- (NSMutableDictionary *)downloadInfo
{
    if (!_downloadInfo) {
        _downloadInfo = [NSMutableDictionary dictionaryWithCapacity:0];
    }
    return _downloadInfo;
}

@end
